package pt.um.jspecput.lib.writer;

import static org.junit.Assert.*;

import java.io.File;
import java.io.Reader;
import java.io.Writer;

import org.databene.benerator.anno.InvocationCount;
import org.databene.feed4junit.Feeder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import pt.um.jspecput.lib.writer.FileHandler;

@RunWith(Feeder.class)
public class FileHandlerTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testConstructor() {
        FileHandler fh = new FileHandler() {
        };

        assertNotNull(fh);
    }

    @Test
    @InvocationCount(10)
    public void testCreateDeleteFile(String fileName) throws Exception {
        File f = new File(fileName);

        FileHandler.createFile(fileName);

        assertTrue(f.exists());

        FileHandler.deleteFile(fileName);

        assertFalse(f.exists());
    }

    @Test
    @InvocationCount(10)
    public void testOpenCloseFileWriter(String fileName) throws Exception {
        Writer writer = FileHandler.openFileWriter(fileName);

        assertTrue(new File(fileName + ".java").exists());

        FileHandler.closeFileWriter(writer);
        FileHandler.deleteFile(fileName + ".java");
    }

    @Test
    @InvocationCount(10)
    public void testOpenCloseFileReader(String fileName) throws Exception {
        Reader reader = FileHandler.openFileReader(fileName);

        assertTrue(new File(fileName + ".java").exists());

        FileHandler.closeFileReader(reader);
        FileHandler.deleteFile(fileName + ".java");
    }
}
